package com.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Replace2 {

	public static void main(String[] args) {
		String s = "/*! Here's   a block of text to use as input to the regular expression matcher. Note that we'll first extract the block of text by looking for the special delimiters. then process the extracted block. !*/";

		Matcher m = Pattern.compile("/\\*!(.*)!\\*/", Pattern.DOTALL)
				.matcher(s);
		if (m.find()) {
			s = m.group(1);
			System.out.println(s);
		}

		s = s.replaceAll(" {2,}", " ");
		System.out.println(s);

		s = s.replaceFirst("[aeiou]", "(VOWELL)");
		System.out.println(s);

		StringBuffer sbuf = new StringBuffer();
		Pattern p = Pattern.compile("[aeiou]");
		m = p.matcher(s);
		// appendReplacement()将当前匹配子串替换为指定字符串，并且将替换后的子串以及其之前到上次匹配子串之后的字符串段添加到一个 StringBuffer 对象里，而
		// appendTail(StringBuffer sb) 方法则将最后一次匹配工作后剩余的字符串添加到一个 StringBuffer
		// 对象里。
		while (m.find()) {
			m.appendReplacement(sbuf, m.group().toUpperCase());
		}
		m.appendTail(sbuf);
		System.out.println(sbuf);
	}

}
